const ARJS = GC.ActiveReports.Core;
const licenseKey =
  "191398738376964#B0ulybaVWSjlWYtNzQyUmU59WaJVma8RUb9dmSDZXNjtUM7tkZURkeut6Z8Z5RXVGNKRTStN4cxkHTzkTY5FESrYVaSdzZ4NXOvw6MUh5c4YHdEVHNwYnN5l5Mz3mUlpUcrEWTygHaj54LhJnSmdEdkxmc9lFZMtUbOJ4Q0tkaqZzKX3UcUNleI9UVVNVbW36SUdEUzNDWwRzdThjZ4l7U8NjYqJ7QDFEbThTcUhzSRVjWrF7MJFGTyITeNd7SzQ4UxEEMktCdktCSv2mM8syUPFGcwQDcTpXVY3COzQzRChWMFVTert6awhHbKhmI0IyUiwiIBZkNzQzM9MjI0ICSiwCN8IDOzYjM4EjM0IicfJye35XX3JSSWFURiojIDJCLiIjVgMlS4J7bwVmUlZXa4NWQiojIOJyebpjIkJHUiwiIwITNwgDMgkTMxATMyAjMiojI4J7QiwiI8+Y9sWY9QmZ0Jyp9Aqo9Re09i+K0oKZ9wap9biY9mKY0L+Y9iojIh94QiwiI4YTO6czM8MzN8kzMxkTMiojIklkIs4XZzxWYmpjIyNHZisnOiwmbBJye0ICRiwiI34zdJh4KHVlMURXSpNDO7dHdk5kTMhkRVpHVwUWSwImWWZFOLRzdxQ7UCV4Vx44T6NFO9ckW5RWewUkZxFWYphlc0hkWNdEc9J5bIVmM54GSZF4dnp6Y7pUZ4gUQXBXQPlmTmVXZ6gHNuhgQ";
ARJS.setLicenseKey(licenseKey);
ARJS.PageReport.LicenseKey = licenseKey;
const fonts = [
  {
    name: "微软雅黑",
    locals: ["Microsoft YaHei", "SimSun Regular"],
    source: "static/fonts/Chinese.ttf",
  },
  {
    name: "Sinhala",
    locals: ["Microsoft YaHei", "SimSun Regular"],
    source: "static/fonts/Chinese.ttf",
  },
  {
    name: "Arial Unicode MS",
    source: "static/fonts/ARIALUNI.TTF",
  },
  {
    name: "Arial Unicode MS",
    weight: "bold",
    source: "static/fonts/ARIALUNIBD.TTF",
  },
  {
    name: "Arial",
    weight: "bold",
    style: "italic",
    source: "static/fonts/ARIALUNIBD.ttf",
  },
  {
    name: "Arial",
    style: "italic",
    source: "static/fonts/ARIALUNI.ttf",
  },
  {
    name: "Arial",
    weight: "bold",
    source: "static/fonts/ARIALUNIBD.ttf",
  },
  {
    name: "Arial",
    source: "static/fonts/ARIALUNI.ttf",
  },
];
ARJS.FontStore.registerFonts(fonts);

const settings = {
  info: {
    title: "Invoice List",
    subject: "This is the Invoice List",
    author: "John K",
    keywords: "PDF; import; export",
  },
  fonts: fonts,
};
const lang = "EN";
function blob2base64(blob) {
  return new Promise((resolve, reject) => {
    var reader = new FileReader();
    reader.onloadend = () => {
      resolve(reader.result);
    };
    reader.onerror = () => {
      reject(reader.err);
    };
    reader.readAsDataURL(blob);
  });
}
async function generateProposal(mainReport, dsparam) {
  const start = new Date();
  const PDF = GC.ActiveReports.PdfExport;
  const pageReport = new ARJS.PageReport();
  return await pageReport
    .load(mainReport)
    .then(() => {
      if ("LicenseError" === pageReport.name) {
        throw new Error("License Error");
      }
      if (pageReport.parameters.dsparam) {
        pageReport.parameters.dsparam.values = [dsparam];
      }
    })
    .then(() => pageReport.run())
    .then((pageDocument) => PDF.exportDocument(pageDocument, settings, (pageNum) => {}))
    .then(async (result) => {
      // const abc = await blob2base64(result.data);
      // return abc;
      // return blob2base64(result.data);
      document.getElementById("pdf").src = URL.createObjectURL(result.data);
    });
}
